
package derby;

import java.util.HashMap;
import java.util.Map;


public class DatabaseConfigFactory {
	
	public enum Database{DERBY_IN_MEMORY, MYSQL};
	
	public interface IDatabaseConfig{
		
		String getUrl(String server, String dbName);
		
		Map<String, String> getProperties();
		
		String getDriver();
		
		String getDialectClassName();
		
		String getLimitString(String sql, int limit);
		
	}
	
	public static IDatabaseConfig createDatabaseConfig(Database database){
		
		switch (database) {
		case DERBY_IN_MEMORY:
			return new DerbyInMemoryDatabaseConfig();
		case MYSQL:
			return new MysqlDatabaseConfig();
		}
		
		return null;
		
	}
	
	private static class MysqlDatabaseConfig implements IDatabaseConfig{

		@Override
    public String getUrl(String server, String dbName) {
	    return "jdbc:mysql://" + server + "/" + dbName;
    }

		@Override
    public Map<String, String> getProperties() {
	    return new HashMap<String, String>();
    }

		@Override
    public String getDriver() {
	    return "com.mysql.jdbc.Driver";
    }

		@Override
    public String getDialectClassName() {
	    return "org.hibernate.dialect.MySQL5Dialect";
    }

		@Override
    public String getLimitString(String sql, int limit) {
	    return "SELECT * FROM ("+sql+") WHERE ROWNUM <= "+limit;
    }
		
	}
	
	private static class DerbyInMemoryDatabaseConfig implements IDatabaseConfig{

		
		@Override
    public String getUrl(String server, String dbName) {
	    return "jdbc:derby:memory:"+dbName+";create=true";
    }

		@Override
    public Map<String, String> getProperties() {
	    
			Map<String, String> map = new HashMap<String, String>(); 
			
			map.put("hibernate.hbm2ddl.auto", "create");
			
			return map;
    }

		@Override
    public String getDriver() {
	    return "org.apache.derby.jdbc.EmbeddedDriver";
    }

		@Override
    public String getDialectClassName() {
	    return "org.hibernate.dialect.DerbyTenSevenDialect";
    }

		@Override
    public String getLimitString(String sql, int limit) {
			return "SELECT * FROM ("+sql+") LIMIT "+limit;
    }
		
	}
	
}
